﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml;

namespace Vision.Core.ActivityComponents.Serialization
{
    /// <summary>
    /// 
    /// </summary>
    public class SerializeObject : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        public InArgument<object> SerializableObject { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Category("Output")]
        public OutArgument<byte[]> OutputFile { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            using (MemoryStream mStream = new MemoryStream()){
                DataContractSerializer serializer = new DataContractSerializer(this.SerializableObject.Get(context).GetType());
                serializer.WriteObject(mStream, this.SerializableObject.Get(context));
                mStream.Position = 0;
                this.OutputFile.Set(context, mStream.ToArray());
            }                                    
        }
    }
}
